package test_hibernate;

import java.util.Collection;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import manager.Base;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Client;
import bean.Ville;
import bean.Voyage;




import sessionHibernate.HibernateUtil;

public class TestHibernate_nn {

	Session sessionHibernate =  null;
	
	public void creerVoyage(String nom) {
		try {
			Transaction tx = sessionHibernate.beginTransaction();
			Voyage v = new Voyage();
			v.setNom(nom);
			sessionHibernate.persist(v);
			tx.commit();
		}
		catch (Exception e) {
			System.out.println("Erreur "+e.getMessage());
		}
	
	}
	
	public void creerVille(String nom) {
		try {
			Transaction tx = sessionHibernate.beginTransaction();
			Ville v = new Ville();
			v.setNom(nom);
			sessionHibernate.persist(v);
			tx.commit();
		}
		catch (Exception e) {
			System.out.println("Erreur "+e.getMessage());
		}
	
	}

	public void listerVoyages() {
		Query query = sessionHibernate.createQuery("from " + Voyage.class.getName());
		List<Voyage> res = query.list();
		for (Voyage v : res) {
			System.out.println("Voyage "+v.getNom());
			System.out.print("  [");
			for (Ville vi : v.getVilles()) {
				System.out.print("-"+vi.getNom()+"-");
			}
			System.out.println("]");
		}
	}
	
	public void listerVilles() {
		Query query = sessionHibernate.createQuery("from " + Ville.class.getName());
		List<Ville> res = query.list();
		for (Ville v : res) {
			System.out.println("Ville "+v.getNom());
			System.out.print("  [");
			for (Voyage vo : v.getVoyages()) {
				System.out.print("-"+vo.getNom()+"-");
			}
			System.out.println("]");
		}
		
	}

	public void go() {
		sessionHibernate =  HibernateUtil.getSession();
		
		creerVoyage(""+new Date());
		creerVille(""+new Date());
		listerVoyages();
		listerVilles();
		
		sessionHibernate.close();

	}
	
	public void getClient(String login){
		sessionHibernate =  HibernateUtil.getSession();
		Client c = Base.getClient(login, sessionHibernate);
		if(c!=null) System.out.println(c.getPassword());
		else  System.out.println("Client null");
		sessionHibernate.close();
	}
	
	public static void main(String [] args) {
		TestHibernate_nn th = new TestHibernate_nn();
		th.getClient("test");
	}
}
